#!/bin/bash
clear
if [[ "$EUID" -ne 0 ]]; then
  echo -e "\033[1;31m Anda tiada kebenaran untuk menjalankan skrip ini! \033[0m"
  exit 1
fi

TEXT='\033[30;107;2m'
GREEN='\033[92m'
YELLOW='\033[93m'
ERROR='\033[97;41m'
RESET='\033[0m'

ipAddr=$(wget -qO- ipv4.icanhazip.com)
hostAddr=$(cat /usr/local/.environment | grep 'DOMAIN' | cut -d ' ' -f 2)

until [[ $UserName =~ ^[a-zA-Z0-9_]+$ && $checkUser == 0 ]]; do
  read -p "Masukkan nama pengguna: " UserName
  checkUser=$(grep -w $UserName /usr/local/etc/v2ray/clients/.accounts | wc -l)
  if [[ $checkUser -ne 0 ]]; then
    echo -e "${ERROR} RALAT ${RESET}${TEXT} Nama pengguna sudah wujud! ${RESET}"
    exit 1
  fi
done
read -p "Masukkan tempoh aktif [Hari]: " DurationTime
expireDate=$(date -d "$DurationTime days" +%F)
clientUUID=$(uuidgen -r)
clientLevel=$(shuf -i 1-99 -n1)
clientEmail=${UserName}@${hostAddr}
clientProto='vless'
vlessNetwork='tcp'
vlessSecurity='tls'
vlessPort=''

if [[ -f /usr/local/etc/v2ray/clients/.accounts ]]; then
  echo -e "${UserName}\t${clientProto}\t${clientUUID}\t${expireDate}" >>/usr/local/etc/v2ray/clients/.accounts
fi

cat /usr/local/etc/v2ray/vless-tcp.json | jq '.inbounds[0].settings.clients += [{"id": "'${clientUUID}'","lavel": '${clientLevel}',"email": "'${clientEmail}'"}]' >/usr/local/etc/v2ray/vless-tcp.json.tmp
mv -f /usr/local/etc/v2ray/vless-tcp.json.tmp /usr/local/etc/v2ray/vless-tcp.json
systemctl restart v2ray@vless-tcp

vless_file="https://$hostAddr/${UserName}_vless-tcp.json"

clear
echo
echo -e "${TEXT} ========================================================== ${RESET}"
echo -e "${TEXT} VLESS - BUAT AKAUN                                         ${RESET}"
echo -e "${TEXT} ---------------------------------------------------------- ${RESET}"
echo
echo -e "${YELLOW} Nama pengguna   :${RESET} ${GREEN}$UserName${RESET}"
echo -e "${YELLOW} Alamat IP       :${RESET} ${GREEN}$ipAddr${RESET}"
echo -e "${YELLOW} Nama domain     :${RESET} ${GREEN}$hostAddr${RESET}"
echo -e "${YELLOW} Protokol        :${RESET} ${GREEN}$clientProto${RESET}"
echo -e "${YELLOW} Rangkaian       :${RESET} ${GREEN}$vlessNetwork${RESET}"
echo -e "${YELLOW} Keselamatan     :${RESET} ${GREEN}$vlessSecurity${RESET}"
echo -e "${YELLOW} Alamat port     :${RESET} ${GREEN}$vlessPort${RESET}"
echo -e "${YELLOW} Kata laluan/uuid:${RESET} ${GREEN}$clientUUID${RESET}"
echo -e "${YELLOW} Tempoh aktif    :${RESET} ${GREEN}$DurationTime hari${RESET}"
echo -e "${YELLOW} Tarikh luput    :${RESET} ${GREEN}$expireDate${RESET}"
echo
echo -e "${GREEN} $vless_link ${RESET}"
echo -e "${GREEN} $vless_file ${RESET}"
echo
echo -e "${TEXT} ---------------------------------------------------------- ${RESET}"
echo -e "${TEXT} Dicipta oleh Doctype, Dikuasakan oleh Cybertize.           ${RESET}"
echo -e "${TEXT} ========================================================== ${RESET}"
echo
